<template>
    <div class="crm-project-process-container">
        <el-dialog title="新增过程" v-model="isShowDialog" append-to-body :close-on-click-modal="false">
            <el-form ref="addCrmProjectProcessFormRef" :model="formData" :rules="formRules" label-width="80px">
                <el-row :gutter="35">
                    <el-col>
                        <el-form-item label="过程名称" prop="processName">
                            <el-input v-model="formData.processName" placeholder="请输入过程名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="过程内容" prop="processText">
                            <el-input v-model="formData.processText" placeholder="请输入过程内容" type="textarea" :rows="5" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="执行人" prop="executorIds">
                            <el-select v-model="formData.executorIds" multiple filterable remote 
                                @change="handleExecutorChange"
                                @click="loadCrmUserData()"
                                :remote-method="loadCrmUserData" placeholder="请选择执行人" clearable class="w100">
								<el-option 
									v-for="item in users"
									:key="item.id"
									:label="item.userName"
									:value="item.id">
								</el-option>
							</el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="开始时间" prop="processStartTime">
                            <el-date-picker v-model="formData.processStartTime" placeholder="请输入开始时间" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" clearable class="w100"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="结束时间" prop="processEndTime">
                            <el-date-picker v-model="formData.processEndTime" placeholder="请输入结束时间" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" clearable class="w100"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" placeholder="请输入备注" type="textarea" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onSubmit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { addCrmProjectProcess } from '/@/api/crm/project/process/index';
import { getCrmUserList } from '/@/api/crm/user/index';
export default {
    name: 'crm/process/add',
    setup(props, context) {
        const addCrmProjectProcessFormRef = ref();
        const state = reactive({
            isShowDialog: false,
            users: [],
            executors: [],
            formData: {
                projectId: undefined,
            },
            formRules : {
                processName: { required: true, message: '过程名称不能为空', trigger: 'blur' },
                processText: { required: true, message: '过程内容不能为空', trigger: 'blur' },
            }
        });

        const loadCrmUserData = (query?: string) => {
            if(query == '') return;
            getCrmUserList({userName: query}).then((res) => {
                if(res.code == 0){
                    state.users = [];
                    state.users = res.data;
                }
            });
        }

        const handleExecutorChange = (executorIds) => {
            state.executors = [];
            const members = state.users.reduce((acc, item) => {
                if (executorIds.includes(item.id) && !acc.some(member => member.id === item.id)) {
                    acc.push(item);
                }
                return acc;
            }, []);
            if(members && members.length > 0){
                members.forEach(member => {
                    state.executors.push({
                        executorId: member.id,
                        executorName: member.userName,
                    });
                })
            }
        }

        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
            state.formData.projectId = row?.projectId;
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            addCrmProjectProcessFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            addCrmProjectProcessFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        projectId: state.formData.projectId,
                        processName: state.formData.processName,
                        processText: state.formData.processText,
                        processStartTime: state.formData.processStartTime,
                        processEndTime: state.formData.processEndTime,
                        remark: state.formData.remark,
                        processExecutors: [...state.executors]
                    }
                    addCrmProjectProcess(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            addCrmProjectProcessFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            loadCrmUserData,
            handleExecutorChange,
            ...toRefs(state),
        };
    },
};
</script>